package com.navinfo.opentsp.platform.computing.serializer;

import com.google.common.base.Charsets;
import com.navinfo.opentsp.platform.computing.util.DateUtils;
import com.navinfo.opentsp.platform.location.protocol.common.LCChassisDataUpload;
import com.navinfo.opentsp.platform.location.protocol.common.LCFaultUpload;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.serialization.EventSerializer;
import org.msgpack.MessagePack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStream;

/**
* FaultUpload信息 A203协议数据上传，
* HDFS_Sink的FaultUpload序列化器，拼接成字段间通过\001分割的字符串
*/
public class HDFSFaultUploadSerializer implements EventSerializer {
    private static final Logger logger = LoggerFactory.getLogger(HDFSFaultUploadSerializer.class);

    private final String APPEND_NEWLINE = "appendNewline";
    private final boolean appendNewline;
    private final String splitStr = "\001";
    private final OutputStream out;

    private MessagePack msgpack = new MessagePack();
    private HDFSFaultUploadSerializer(OutputStream out, Context ctx) {
        this.appendNewline = ctx.getBoolean(APPEND_NEWLINE, true);
        this.out = out;
    }

    @Override
    public boolean supportsReopen() {
        return false;
    }
    @Override
    public void afterCreate() {}
    @Override
    public void afterReopen() {}
    @Override
    public void beforeClose() {}
    @Override
    public void flush() throws IOException {
        this.out.flush();
    }
    @Override
    public void write(Event event) throws IOException {
        try {
//            logger.error(event.toString());
            //拼写落盘到HDFS的字符串。字段间分隔符是\001，null通过\N表示
            LCFaultUpload.FaultUpload faultUpload = LCFaultUpload.FaultUpload.parseFrom(event.getBody());
            StringBuilder faultUploadStr = new StringBuilder(5000);

            faultUploadStr.append(faultUpload.getLatitude()).append(splitStr);
            faultUploadStr.append(faultUpload.getLongitude()).append(splitStr);
            faultUploadStr.append(faultUpload.getOriginalLat()).append(splitStr);
            faultUploadStr.append(faultUpload.getOriginalLng()).append(splitStr);
            faultUploadStr.append(faultUpload.getHeight()).append(splitStr);
            faultUploadStr.append(faultUpload.getDirection()).append(splitStr);
            faultUploadStr.append(faultUpload.getGpsDate()).append(splitStr);
            faultUploadStr.append(faultUpload.getFaultStatus()).append(splitStr);
            faultUploadStr.append(faultUpload.getFaultAddress()).append(splitStr);
            faultUploadStr.append(faultUpload.getFaultContent()).append(splitStr);
            faultUploadStr.append(faultUpload.getTotalDriveMileage()).append(splitStr);
            faultUploadStr.append(faultUpload.getGpsSpeed()).append(splitStr);
            faultUploadStr.append(faultUpload.getSourceAddressCnt()).append(splitStr);
            faultUploadStr.append(faultUpload.getSourceAddress()).append(splitStr);
            faultUploadStr.append(faultUpload.getCollectDate()).append(splitStr);
            faultUploadStr.append(faultUpload.getDataLength()).append(splitStr);
            faultUploadStr.append(faultUpload.getAtmosphericPressure()).append(splitStr);
            faultUploadStr.append(faultUpload.getAtmosphericTemperature()).append(splitStr);
            faultUploadStr.append(faultUpload.getFakeThrottleOpening()).append(splitStr);
            faultUploadStr.append(faultUpload.getT50Status()).append(splitStr);
            faultUploadStr.append(faultUpload.getThrottleOpening()).append(splitStr);
            faultUploadStr.append(faultUpload.getDashboardSpeed()).append(splitStr);
            faultUploadStr.append(faultUpload.getTotalMileage()).append(splitStr);
            faultUploadStr.append(faultUpload.getSubTotalMileage()).append(splitStr);
            faultUploadStr.append(faultUpload.getIntakeAirTemperature()).append(splitStr);
            faultUploadStr.append(faultUpload.getExhaustGasTemperature()).append(splitStr);
            faultUploadStr.append(faultUpload.getNoxInContr()).append(splitStr);
            faultUploadStr.append(faultUpload.getDpfPressureDiff()).append(splitStr);
            faultUploadStr.append(faultUpload.getNoxOutContr()).append(splitStr);
            faultUploadStr.append(faultUpload.getUreaLevel()).append(splitStr);
            faultUploadStr.append(faultUpload.getUreaTemp()).append(splitStr);
            faultUploadStr.append(faultUpload.getReferTorque()).append(splitStr);
            faultUploadStr.append(faultUpload.getEgrStatus()).append(splitStr);
            faultUploadStr.append(faultUpload.getActualOutputTorque()).append(splitStr);
            faultUploadStr.append(faultUpload.getEngineSpeed()).append(splitStr);
            faultUploadStr.append(faultUpload.getOutFlow()).append(splitStr);
            faultUploadStr.append(faultUpload.getInNox()).append(splitStr);
            faultUploadStr.append(faultUpload.getOutNox()).append(splitStr);
            faultUploadStr.append(faultUpload.getInFlow()).append(splitStr);
            faultUploadStr.append(faultUpload.getWaterTemperature()).append(splitStr);
            faultUploadStr.append(faultUpload.getCurrentGear()).append(splitStr);
            faultUploadStr.append(faultUpload.getIntakeManifoldBoostPressure()).append(splitStr);
            faultUploadStr.append(faultUpload.getIntakeManifoldBoostAirTemperature()).append(splitStr);
            faultUploadStr.append(faultUpload.getOuttakeAirTemperature()).append(splitStr);
            faultUploadStr.append(faultUpload.getRealTimeOilConsumption()).append(splitStr);
            faultUploadStr.append(faultUpload.getAverageFuelConsumption()).append(splitStr);
            faultUploadStr.append(faultUpload.getThrottle1Valve()).append(splitStr);
            faultUploadStr.append(faultUpload.getEecuGear()).append(splitStr);
            faultUploadStr.append(faultUpload.getOilPressure()).append(splitStr);
            faultUploadStr.append(faultUpload.getTerminalId()).append(splitStr);


            //添加字段只能在分割线之上添加,注意时间分区这块，终端传的时候是秒还是毫秒
            //------------------------------------------分割线----------------------------------------
            faultUploadStr.append(DateUtils.format(faultUpload.getGpsDate() * 1000, "yyyyMMdd"));
//            logger.error(dataJson);
            //将数据拼接后写入hdfs
            this.out.write(faultUploadStr.toString().getBytes(Charsets.UTF_8));
            if (this.appendNewline) {
                this.out.write(10);
            }
        }catch (Exception e){
            logger.error("执行HDFS的HDFSFaultUploadSerializer拦截器失败：" + e.getMessage(), e);
        }
    }

//    public static List<String> getMaintenanceEvents(List<LCLocationData.StaytimeParkingAddition> staytimeParkings){
//        List<String> staytimeParkingDtos = Lists.newArrayList();
//        if(staytimeParkings == null){
//            return staytimeParkingDtos;
//        }
//        for(LCLocationData.StaytimeParkingAddition staytimeParking : staytimeParkings){
//            staytimeParkingDtos.add(StaytimeParkingAdditionDto.parseString(staytimeParking));
//        }
//        return staytimeParkingDtos;
//    }


    public static class Builder implements EventSerializer.Builder {
        public Builder() { }
        @Override
        public EventSerializer build(Context context, OutputStream out) {
            return  new HDFSFaultUploadSerializer(out, context);
        }
    }
}
